#include "MessageQueue.h"
#include <iostream>

MessageQueue::MessageQueue()
{
	pthread_mutex_init(&mutex, NULL);
}

MessageQueue::~MessageQueue()
{

}

void MessageQueue::push_message(struct Message message)
{
 	pthread_mutex_lock(&mutex);
	message_queue.push(message);
	pthread_mutex_unlock(&mutex);

	pthread_cond_signal(&count_nonzero);

	std::cout << "push message" << std::endl;
}

struct Message MessageQueue::pop_message()
{
	pthread_mutex_lock(&mutex);
    while(message_queue.empty()) 
        pthread_cond_wait(&count_nonzero, &mutex);

    struct Message message = message_queue.front();
    message_queue.pop();

    pthread_mutex_unlock(&mutex);

    std::cout << "pop message" << std::endl;

    return message;
}